﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {


	private GameObject gunPoint;
	private GameObject playerShadow;
	private GameObject dirtPuff;
	private GameObject xrayMask;
	private GameObject waterSplash;

	[HideInInspector]
	public int playerFloor;

	private Rigidbody2D rb2D;
	private Animator anim;
	private AudioSource soundClip;

	private enum PlayerDirection {North, East, South, West, NorthEast, NorthWest, SouthEast, SouthWest};
	private PlayerDirection playerDirection;
	private Vector2 moveDir;
	private bool playerMoving = false;
	//private bool playerShooting = false;
	private int playerAngle;

	[SerializeField]
	private float playerSpeed;

	//--------------------------------------------------------------------------
	// INITIALIZATION
	//--------------------------------------------------------------------------
	void Start () 
	{
		rb2D = GetComponent<Rigidbody2D>();
		anim = GetComponent<Animator>();
		soundClip = GetComponent<AudioSource>();

		gunPoint = transform.Find("GunPoint").gameObject;
		playerShadow = transform.Find("PlayerShadow").gameObject;
		dirtPuff = transform.Find("DirtPuff").gameObject;
		xrayMask = transform.Find("xraymask").gameObject;
		waterSplash = transform.Find("watersplash").gameObject;

		playerDirection = PlayerDirection.South;
		playerFloor = 0;
		xrayMask.SetActive(false);
		waterSplash.SetActive(false);

	}

	//--------------------------------------------------------------------------
	// PLAYER MOVEMENT
	//--------------------------------------------------------------------------
	void FixedUpdate()
	{
		// Store Input and normalize vector for consistant speed on diagonals
		moveDir = new Vector2(Input.GetAxisRaw ("Horizontal"),Input.GetAxisRaw ("Vertical")).normalized;
		// Move the player
		rb2D.MovePosition(rb2D.position + moveDir * (playerSpeed * Time.fixedDeltaTime));
	}


	void Update () 
	{

		//--------------------------------------------------------------------------
		// PLAYER DIRECTION AND ANIMATION
		//--------------------------------------------------------------------------
		//Get Inputs
		float moveH = Input.GetAxisRaw ("Horizontal");
		float moveV = Input.GetAxisRaw ("Vertical");

		//Set the player direction based on user inputs
		if(moveH == 0 && moveV == 1){ playerDirection = PlayerDirection.North; playerMoving = true; }
		if(moveH == 0 && moveV == -1){ playerDirection = PlayerDirection.South; playerMoving = true; }
		if(moveH == 1 && moveV == 0){ playerDirection = PlayerDirection.East; playerMoving = true; }
		if(moveH == -1 && moveV == 0){ playerDirection = PlayerDirection.West; playerMoving = true; }
		if(moveH == 1 && moveV == 1){ playerDirection = PlayerDirection.NorthEast; playerMoving = true; }
		if(moveH == -1 && moveV == 1){ playerDirection = PlayerDirection.NorthWest; playerMoving = true; }
		if(moveH == 1 && moveV == -1){ playerDirection = PlayerDirection.SouthEast; playerMoving = true; }
		if(moveH == -1 && moveV == -1){ playerDirection = PlayerDirection.SouthWest; playerMoving = true; }

		//Based on Direction - If the player is moving we play the moving animation
		//otherwise play the idle animation. We also set the angle based on direction that
		//can be used by other things that need the player direction.
		switch (playerDirection)
		{
		case PlayerDirection.North:
			if (playerMoving == true){anim.Play("Matt_N_Run");} else {anim.Play("Matt_N_Idle"); }
			playerMoving = false;
			playerAngle = 90;
			break;
		case PlayerDirection.South:
			if(playerMoving == true){anim.Play("Matt_S_Run");} else {anim.Play("Matt_S_Idle"); }
			playerMoving = false;
			playerAngle = -90;
			break;
		case PlayerDirection.East:
			if(playerMoving == true){anim.Play("Matt_E_Run");} else {anim.Play("Matt_E_Idle");}
			playerMoving = false;
			playerAngle = 0;
			break;
		case PlayerDirection.West:
			if(playerMoving == true){anim.Play("Matt_W_Run");} else {anim.Play("Matt_W_Idle");}
			playerMoving = false; 
			playerAngle = 180;
			break;
		case PlayerDirection.NorthEast:
			if(playerMoving == true){anim.Play("Matt_NE_Run");} else {anim.Play("Matt_NE_Idle");}
			playerMoving = false;
			playerAngle = 45;
			break;
		case PlayerDirection.NorthWest:
			if(playerMoving == true){anim.Play("Matt_NW_Run");} else {anim.Play("Matt_NW_Idle");}
			playerMoving = false;
			playerAngle = 135;
			break;
		case PlayerDirection.SouthEast:
			if(playerMoving == true){anim.Play("Matt_SE_Run");} else {anim.Play("Matt_SE_Idle");}
			playerMoving = false;
			playerAngle = -45;
			break;
		case PlayerDirection.SouthWest:
			if(playerMoving == true){anim.Play("Matt_SW_Run");} else {anim.Play("Matt_SW_Idle");}
			playerMoving = false;
			playerAngle = -135;
			break;
			default:
			print("playerDirection default");
			break;
		}

		//--------------------------------------------------------------------------
		// PLAYER SHOOTING
		//--------------------------------------------------------------------------
		// We get the user input both Project Input Settings and custom imput key.
		if(Input.GetButtonDown("Fire1") || (Input.GetKeyDown(KeyCode.Z)) == true)
		{
			// Note we just request a projectile whether it fires or not
			// Spawn the projectile
			PopShotController.Spawn(gunPoint.transform.position, playerAngle);
			//Spawn the firing effect for the specific projectile
			PopShotFlashController.Spawn(gunPoint.transform.position);
			soundClip.PlayOneShot(soundClip.clip);


		}

	}

	//--------------------------------------------------------------------------
	// PLAYER TRIGGERS AND COLLISIONS
	//--------------------------------------------------------------------------
	protected void OnTriggerEnter2D(Collider2D collider)
	{
		// Set the player floor when going up and down steps
		if(collider.gameObject.tag == "PlayerStep")
		{
			if(playerFloor == 0)
			{
				playerFloor = 1;
			}
			else
			{
				playerFloor = 0;
			}
		}

		//Turns on the mask
		if(collider.gameObject.tag == "Xray")
		{
			xrayMask.SetActive(true);
			waterSplash.SetActive(true);
			playerShadow.SetActive(false);
			dirtPuff.SetActive(false);
		}
	}

	protected void OnTriggerExit2D(Collider2D collider)
	{
		if(collider.gameObject.tag == "Xray")
		{
			xrayMask.SetActive(false);
			waterSplash.SetActive(false);
			playerShadow.SetActive(true);
			dirtPuff.SetActive(true);
		}
	}
}
